package com.example.graduationdesignbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.FeedbackDto;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.Feedback;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.FeedbackMapper;
import com.example.graduationdesignbackend.query.FeedbackQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 在线留言 服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-04-16
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

  @Override
  public Res<String> createFeedback(FeedbackDto feedbackDto) {
    Feedback feedback = new Feedback();
    feedback.setId(RandomUtil.randomNumbers(9));
    feedback.setCreateTime(new Date().getTime());
    feedback.setPhone(feedbackDto.getPhone());
    feedback.setMessage(feedbackDto.getMessage());
    feedback.setRealname(feedbackDto.getRealname());
    feedback.setCheckStatus(0);
    Boolean save = save(feedback);
    if (!save){
      throw new ParkException(ResponseCode.SaveError);
    }
    return Res.yuze("创建成功！");
  }

  @Override
  public Feedback getInfo(String id) {
    Feedback feedback = getById(id);
    feedback.setCheckStatus(1);
    feedback.setUpdateTime(new Date().getTime());
    Boolean update = updateById(feedback);
    if (!update) throw new ParkException(ResponseCode.UpdateError);
    return feedback;
  }

  @Override
  public PageDto<Feedback> getFeedbackPages(FeedbackQuery feedbackQuery) {
    String key = feedbackQuery.getKey();
    int current = 1;
    if (!BeanUtil.isEmpty(feedbackQuery.getCurrent())){
      current = feedbackQuery.getCurrent();
    }
    int pageSize = 10;
    if (!BeanUtil.isEmpty(feedbackQuery.getPageSize())){
      pageSize = feedbackQuery.getPageSize();
    }
    Page<Feedback> page = Page.of(current,pageSize);
    page.addOrder(new OrderItem("create_time",false));
    Page<Feedback> p = new Page<>();
    if (BeanUtil.isEmpty(feedbackQuery.getKey())){
      p = lambdaQuery().page(page);
    }else {
      p = lambdaQuery()
        .like(Feedback::getRealname,key)
        .or()
        .like(Feedback::getPhone,key)
        .page(page);
    }
    PageDto<Feedback> dto = new PageDto<>();
    dto.setTotal(p.getTotal());
    dto.setPages(p.getPages());
    dto.setPageSize(p.getSize());
    dto.setCurrent(p.getCurrent());
    dto.setList(p.getRecords());
    return dto;
  }
}
